package files;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Arrays;

public class ImageFinder {
	
	private File path;
	private String[] types;
	
	public ImageFinder(String strpath, String[] file_types) {
		path = new File(strpath);
		types = file_types;
	}

	public ListFile[] search() {
		FilenameFilter img_filter = new ImageFilter(types);
		FileFilter dir_filter = new DirFilter();
		ListFile[] files = ListFile.files2ListFiles(path.listFiles(img_filter));
		ListFile[] sub_dirs = ListFile.files2ListFiles(path.listFiles(dir_filter));
		ListFile[] all = files;
		for(int i = 0; i < sub_dirs.length; i++) {
			ImageFinder tmp_finder = new ImageFinder(sub_dirs[i].getPath(),types); 
			ListFile[] tmp_files = tmp_finder.search();
			all = concat(all,tmp_files);
		}
		return all;
	}
	
	private ListFile[] concat(ListFile[] first, ListFile[] second) {
		ListFile[] result = Arrays.copyOf(first, first.length + second.length);
		System.arraycopy(second, 0, result, first.length, second.length);
		return result;
	}
}